// MemDumpBox.cpp: implementation of the CMemDumpBox class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "8085.h"
#include "MemDumpBox.h"
#include "NumericBox.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CMemDumpBox::CMemDumpBox()
{
    Init();
}

CMemDumpBox::~CMemDumpBox()
{

}

void CMemDumpBox::Init()
{
    m_nFirstAddress = 0;
    m_nMarginWidth = 3;
    SetMaxSize(0xffff);
}

// return the last address
int CMemDumpBox::RefreshStrings(int addr,BOOL bCheckModified)
{
    if(m_aDisplay==NULL) return 0;
    for(int i=0; i<m_nItems ; i++)
    {
        byte data = ReadMemByte(addr+i);
        if(bCheckModified)
        {
            if(m_aDisplay[i].data != data)
                m_aDisplay[i].bModified = 1;
            else m_aDisplay[i].bModified = 0;
        }
        else m_aDisplay[i].bModified = 0;
        sprintf(m_aDisplay[i].szMnemonic,"%04x %02x",addr+i,data);
        m_aDisplay[i].address = addr+i;
        m_aDisplay[i].data = data;
    }
	return m_aDisplay[m_nItems-1].address;
}

// increment the data at the location pointed by m_nSelectedIndex
void CMemDumpBox::IncrementData()
{
    word address = AddressAtIndex(m_nSelectedIndex);
    byte data = ReadMemByte(address)+1;
    WriteMem(address,data);
}

// decrement the data at the location pointed by m_nSelectedIndex
void CMemDumpBox::DecrementData()
{
    word address = AddressAtIndex(m_nSelectedIndex);
    byte data = ReadMemByte(address)-1;
    WriteMem(address,data);
}

void CMemDumpBox::EnterData()
{
    CNumericBox c;
    static char str[32];
    static char szData[32];
    word nAddress = AddressAtIndex(m_nSelectedIndex);
    int nResult;

    sprintf(str,"Change data at 0x%0.4x",nAddress);
    sprintf(szData,"0x%0.4x",ReadMemByte(nAddress));
    c.SetCaption(str);
    if(c.DisplayNumericBox(nResult,0,GetMemSize(),szData))
    {
        WriteMem(nAddress,(byte)nResult);
        //RefreshStrings();
    }
    RefreshStrings();
    InvalidateRect(m_Rect);
}
